package com.cn.aam.checaiduo.ui.me;


import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.aam.checaiduo.AppData;
import com.cn.aam.checaiduo.Const;
import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.backend.RetrofitHome;
import com.cn.aam.checaiduo.backend.observer.OnRequestListener;
import com.cn.aam.checaiduo.backend.rx.RxSchedulers;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.base.web.NormalWebActivity;
import com.cn.aam.checaiduo.bean.me.RedirectContentB;
import com.cn.aam.checaiduo.center.UserCenter;
import com.cn.aam.checaiduo.ui.arouter.UIRouter;
import com.cn.aam.checaiduo.ui.arouter.section.RouterMore;
import com.cn.aam.checaiduo.utils.BankUtil;
import com.cn.aam.checaiduo.utils.MyEditTextUtils;
import com.cn.aam.checaiduo.utils.StringUtil;
import com.cn.aam.checaiduo.widget.WMToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 充值
 */
public class RechargeActivity extends BaseActivity {

    @BindView(R.id.iv_bank_logo)
    ImageView ivBankLogo;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_remark)
    TextView tvBankRemark;

    @BindView(R.id.et_recharge_amount)
    EditText etRechargeAmount;

    public static Intent newInstance(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        return intent;
    }

    public static Intent newInstance(Context context, String amount) {
        Intent intent = new Intent(context, RechargeActivity.class);
        intent.putExtra("amount", amount);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_recharge;
    }


    @Override
    protected void initView() {
        //toolbar
        ToolBarManager.with(this, WMContent).setBackgroundColor(R.color.toolbar_bg_grey)
                .setNavigationIcon(R.drawable.ic_nav_arrow_back)
                .setTitle("充值")
                .setTitleColor(R.color.black_normal)
                .setMenuTextContent("限额说明")
                .setMenuTextColor(R.color.black_normal)
                .setMenuTextClick(new ToolBarManager.OnMenuTextClickListener() {
                    @Override
                    public void onMenuTextClick() {
                        String url = RetrofitHome.HOST_API + "Wap/Usercenter/rechargeLimit.html";
                        startActivity(NormalWebActivity.newInstance(mContext, url, "限额说明"));
                    }
                });

        String bank_cardno = UserCenter.getInstance().getUser().getBank_cardno();
        String bankName = UserCenter.getInstance().getUser().getBank_name();
        String bankTint = UserCenter.getInstance().getUser().getBank_limit();


        if (TextUtils.isEmpty(bankName)) {
            findViewById(R.id.rl_bank).setVisibility(View.GONE);
            return;
        }
        ivBankLogo.setImageResource(BankUtil.getBankIcon(bankName));
        tvBankName.setText(bankName + "（尾号" + bank_cardno.replace("*", "") + "）");
        tvBankRemark.setText(bankTint);
        String amount = getIntent().getStringExtra("amount");
        if (!TextUtils.isEmpty(amount)) {
            etRechargeAmount.setText(StringUtil.dealDataEnd(amount));
            etRechargeAmount.setSelection(etRechargeAmount.getText().length());
        }

    }

    @OnClick(R.id.btn_submit)
    public void onClickSubmit() {

        if (!checkRechargeValue()) {
            return;
        }
        //未绑卡情况下进行充值，如果未实名认证，需要先进行实名认证
        if (!TextUtils.equals(UserCenter.getInstance().getUser().getAuth_status(), Const.AUTH_STATUS)) {
            WMToast.normal("请先进行实名认证");
            UIRouter.getInstance().openPage(mContext, UIRouter.MORE.AUTH);
            return;
        }

        doRecharge();
    }

    /**
     * 检查值
     *
     * @return 通过为 true 不通过为 false
     * @info 此处可以无限增加各种检查条件
     */
    private boolean checkRechargeValue() {
        String rechargeAmount = MyEditTextUtils.getContent(etRechargeAmount);
        if (TextUtils.isEmpty(rechargeAmount)) {
            WMToast.normal("请输入充值金额");
            return false;
        }
        if (rechargeAmount.charAt(0) == '.') {
            WMToast.normal("金额格式有误");
            return false;
        }
        return true;
    }

    /**
     * 充值操作
     */
    private void doRecharge() {
        String rechargeAmount = MyEditTextUtils.getContent(etRechargeAmount);
        RetrofitHome.getInstance().getInternalApi()
                .recharge(AppData.getToken(), rechargeAmount)
                .compose(RxSchedulers.<RedirectContentB>toMain())
                .subscribe(new OnRequestListener<RedirectContentB>(this) {
                    @Override
                    public void onNext(RedirectContentB value) {
                        super.onNext(value);
                        startActivity(NormalWebActivity.newInstance(mContext, value.getRedirect_content(), "充值", "MEFRAGMENT"));
                    }
                });
    }
}
